const mysql = require('mysql');

module.exports = (sql,params=[]) => {
    //1.创建链接对象
    let connection = mysql.createConnection({
        host:'localhost',
        user:'root',
        password:'root',
        database:'1805a'
    })
    //2.链接数据库
    connection.connect((error) => {
        if(error){
            console.log('数据库链接失败')
        }else{
            console.log('数据库链接成功')
        }
    })
    //3.交互
    return new Promise((resolve,reject) => {
        connection.query(sql,params,(error,data) =>{
            if(error){
                reject(error);
            }else{
                resolve(data);
            }
            //4.结束
            connection.end();
        })
    })
    
}